﻿using UnityEngine;
using System.Collections;

/// <summary>
/// 手势插件FingerGestures类
/// 1.手指滑动方向和放缩方式枚举
/// 2.滑动、点击和放缩委托
/// 3.处理放缩（分平台）
/// 4.手指放下抬起的方法
/// 5.滑动开始、结束和滑动中的方法
/// </summary>
public class FingerEvent :  MonoBehaviour {

    public static FingerEvent Instance;

    //手指滑动的方向
    public enum FingerDir
    {
        Left,
        Right,
        Up,
        Down
    }

    //缩放方式
    public enum ZoomType
    {
        In,
        Out
    }

    public System.Action<FingerDir> OnFingerDrag;   //委托：当滑动的时候，通知出去
    public System.Action OnPlayerClickGround;       //委托：玩家点击了地面执行
    public System.Action<ZoomType> OnZoom;          //委托：缩放方式

    public Vector2 m_O1dFingerPos;                  //手指上一次的位置
    public Vector2 m_Dir;                           //滑动中时手指的方向
    public int m_PrevFinger = -1;                   //玩家上一操作

    private Vector2 m_TempFinger1Pos;               //移动触摸变量
    private Vector2 m_TempFinger2Pos;               //移动触摸变量

    private Vector2 m_OldFinger1Pos;                //移动触摸变量
    private Vector2 m_OldFinger2Pos;                //移动触摸变量

    void Awake()
    {
        Instance = this;
    }

    void Update()
    {
        //处理摄像机放缩
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
        if(Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            if(OnZoom != null) { OnZoom(ZoomType.In); }
        }
        else if(Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            if(OnZoom != null) { OnZoom(ZoomType.Out); }
        }
#elif UNITY_ANDROID || UNITY_IPHONE
        if (Input.touchCount > 1)
        {
            //如果两个手指操作，并且至少有一个手指在移动
            if(Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
            {
                m_TempFinger1Pos = Input.GetTouch(0).position;
                m_TempFinger2Pos = Input.GetTouch(1).position;

                if (Vector2.Distance(m_OldFinger1Pos, m_OldFinger2Pos) < Vector2.Distance(m_TempFinger1Pos, m_TempFinger2Pos))
                {
                    //放大
                    if (OnZoom != null) { OnZoom(ZoomType.In); }
                }
                else
                {
                    //缩小
                    if (OnZoom != null) { OnZoom(ZoomType.Out); }
                }

                m_OldFinger1Pos = m_TempFinger1Pos;
                m_OldFinger2Pos = m_TempFinger2Pos;
            }
        }
#endif
    }

    void OnEnable()
    {
    	//启动时调用，这里开始注册手势操作的事件。
    	//按下事件： OnFingerDown就是按下事件监听的方法，这个名子可以由你来自定义。方法只能在本类中监听。下面所有的事件都一样！！！
        FingerGestures.OnFingerDown += OnFingerDown;
        
        //抬起事件
		FingerGestures.OnFingerUp += OnFingerUp;
	    
        //开始拖动事件
	    FingerGestures.OnFingerDragBegin += OnFingerDragBegin;
        
        //拖动中事件...
        FingerGestures.OnFingerDragMove += OnFingerDragMove;
        //拖动结束事件
        FingerGestures.OnFingerDragEnd += OnFingerDragEnd; 
		
        //上、下、左、右、四个方向的手势滑动
		//FingerGestures.OnFingerSwipe += OnFingerSwipe;
		
        //连击事件 连续点击事件
		//FingerGestures.OnFingerTap += OnFingerTap;
		
        //按下事件后调用一下三个方法
		FingerGestures.OnFingerStationaryBegin += OnFingerStationaryBegin;
		//FingerGestures.OnFingerStationary += OnFingerStationary;
		//FingerGestures.OnFingerStationaryEnd += OnFingerStationaryEnd;
		
        //长按事件
		//FingerGestures.OnFingerLongPress += OnFingerLongPress;
		
    }

    void OnDisable()
    {
    	//关闭时调用，这里销毁手势操作的事件
    	//和上面一样
        FingerGestures.OnFingerDown -= OnFingerDown;
		FingerGestures.OnFingerUp -= OnFingerUp;
		FingerGestures.OnFingerDragBegin -= OnFingerDragBegin;
        FingerGestures.OnFingerDragMove -= OnFingerDragMove;
        FingerGestures.OnFingerDragEnd -= OnFingerDragEnd; 
		//FingerGestures.OnFingerSwipe -= OnFingerSwipe;
		//FingerGestures.OnFingerTap -= OnFingerTap;
		FingerGestures.OnFingerStationaryBegin -= OnFingerStationaryBegin;
		//FingerGestures.OnFingerStationary -= OnFingerStationary;
		//FingerGestures.OnFingerStationaryEnd -= OnFingerStationaryEnd;
		//FingerGestures.OnFingerLongPress -= OnFingerLongPress;
    }

    //按下时调用
    void OnFingerDown( int fingerIndex, Vector2 fingerPos )
    {
        m_PrevFinger = 1;
		//int fingerIndex 是手指的ID 第一按下的手指就是 0 第二个按下的手指就是1。。。一次类推。
		//Vector2 fingerPos 手指按下屏幕中的2D坐标
		
		//将2D坐标转换成3D坐标
        //transform.position = GetWorldPos( fingerPos );
		//Debug.Log(" OnFingerDown ="  +fingerPos); 
    }
	
	//抬起时调用
	void OnFingerUp( int fingerIndex, Vector2 fingerPos, float timeHeldDown )
	{
		if(m_PrevFinger == 1)
        {
            m_PrevFinger = -1;
            if(OnPlayerClickGround != null)
            {
                OnPlayerClickGround();
            }
        }
		//Debug.Log(" OnFingerUp ="  +fingerPos); 
	}
	
	//开始滑动
	void OnFingerDragBegin( int fingerIndex, Vector2 fingerPos, Vector2 startPos )
    {
        m_PrevFinger = 2;
        m_O1dFingerPos = fingerPos;
        //Debug.Log("OnFingerDragBegin fingerIndex =" + fingerIndex  + " fingerPos ="+fingerPos +"startPos =" +startPos); 
    }

    //滑动结束
    void OnFingerDragEnd( int fingerIndex, Vector2 fingerPos )
	{
        m_PrevFinger = 4;
	 	//Debug.Log("OnFingerDragEnd fingerIndex =" + fingerIndex  + " fingerPos ="+fingerPos); 
	}

	//滑动中
    void OnFingerDragMove( int fingerIndex, Vector2 fingerPos, Vector2 delta )
    {
        m_PrevFinger = 3;
        //transform.position = GetWorldPos( fingerPos );
        //Debug.Log(" OnFingerDragMove ="  +fingerPos); 
        //Debug.Log("滑动中");
        m_Dir = fingerPos - m_O1dFingerPos;     //当前位置 - 上一个位置

        //根据方向识别，手指向哪个方向滑动
        if (m_Dir.y < m_Dir.x && m_Dir.y > -m_Dir.x)
        {
            //向右
            Debug.Log("向右");
            if (OnFingerDrag != null)
            {
                OnFingerDrag(FingerDir.Right);  //运行委托
            }
        }
        else if (m_Dir.y > m_Dir.x && m_Dir.y < -m_Dir.x)
        {
            //向左
            Debug.Log("向左");
            if (OnFingerDrag != null)
            {
                OnFingerDrag(FingerDir.Left);  //运行委托
            }
        }
        else if (m_Dir.y > m_Dir.x && m_Dir.y > -m_Dir.x)
        {
            //向上
            Debug.Log("向上");
            if (OnFingerDrag != null)
            {
                OnFingerDrag(FingerDir.Up);  //运行委托
            }
        }
        else
        {
            //向下
            Debug.Log("向下");
            if (OnFingerDrag != null)
            {
                OnFingerDrag(FingerDir.Down);  //运行委托
            }
        }
    }

	//上下左右四方方向滑动手势操作
	//void OnFingerSwipe( int fingerIndex, Vector2 startPos, FingerGestures.SwipeDirection direction, float velocity )
    //{
	//    //结果是 Up Down Left Right 四个方向
	//    Debug.Log("OnFingerSwipe " + direction + " with finger " + fingerIndex);
    //}
	
	//连续按下事件， tapCount就是当前连续按下几次
	//void OnFingerTap( int fingerIndex, Vector2 fingerPos, int tapCount )
    //{		
    //    //Debug.Log("OnFingerTap " + tapCount + " times with finger " + fingerIndex);
    //}
	
	//按下事件开始后调用，包括 开始 结束 持续中状态只到下次事件开始！
	void OnFingerStationaryBegin( int fingerIndex, Vector2 fingerPos )
	{
        //Debug.Log("OnFingerStationaryBegin " + fingerPos + " times with finger " + fingerIndex);
	}

    //void OnFingerStationary( int fingerIndex, Vector2 fingerPos, float elapsedTime )
    //{
    //	 Debug.Log("OnFingerStationary " + fingerPos + " times with finger " + fingerIndex);
    //}

    //void OnFingerStationaryEnd( int fingerIndex, Vector2 fingerPos, float elapsedTime )
    //{
    //	 Debug.Log("OnFingerStationaryEnd " + fingerPos + " times with finger " + fingerIndex);
    //}


    //长按事件
    //void OnFingerLongPress( int fingerIndex, Vector2 fingerPos )
    //{

    //	Debug.Log("OnFingerLongPress " + fingerPos );
    //}

    //把Unity屏幕坐标换算成3D坐标
    //Vector3 GetWorldPos(Vector2 screenPos)
    //{
    //    Camera mainCamera = Camera.main;
    //    return mainCamera.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, Mathf.Abs(transform.position.z - mainCamera.transform.position.z)));
    //}
}
